package addCompo.ub
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getDefinitionByName;
	
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 강석민
	 */	
	public class XMUniversalButton extends MovieClip
	{
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		public var defaultSkin : DefaultSkin;
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var currentSkin : MovieClip;
		private var labelTF : TextField = new TextField();
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function XMUniversalButton()
		{
			super();
			
			defaultSkin = new DefaultSkin();
			currentSkin = defaultSkin;
			
			addSkin( currentSkin );
			
			//
			var tf : TextFormat = new TextFormat( "돋움", 12, 0x666666, true );
			
			labelTF.defaultTextFormat = tf;
			labelTF.autoSize = TextFieldAutoSize.LEFT;
			labelTF.selectable = false;
			labelTF.mouseEnabled = false;
			this.parent.addChild( labelTF);
			labelTF.text = "Button";
			
			tfPositioning();
			
		}
		
		
		
		[Inspectable(name="Label", defaultValue = "Button", type = String )]
		public function set Label ( str : String ):void
		{
			labelTF.text = str;
			tfPositioning();
		}
		
		[Inspectable(name="LabelColor", defaultValue="#666666", type=Color )]
		public function set LabelColor ( value : Number ):void
		{
			var tf : TextFormat = new TextFormat();
			tf.color = value;
			
			labelTF.setTextFormat( tf );
		}
		
		[Inspectable(name="LabelSize", defaultValue="12", type=Number )]
		public function set LabelSize ( value : Number ):void
		{
			var tf : TextFormat = new TextFormat();
			tf.size = int(value);
			
			labelTF.setTextFormat( tf );
			tfPositioning();
			
		}
		
		
		[Inspectable(name="SkinClass", defaultValue="DefaultValue", type=String )]
		public function set SkinClass ( value : String ):void
		{
			var makeClass : Class;
			var makeMovieClip : MovieClip;
			
			//생성
			try
			{
				makeClass = getDefinitionByName( value ) as Class;
				makeMovieClip = new makeClass() as MovieClip;
				makeMovieClip.stop();
			} 
			catch(error:Error) 
			{
				//정의 되지 않은 클래스 이름일때
				makeMovieClip = defaultSkin;
			}
			//헌거 떼어 버리고
			removeSkin( currentSkin );
			
			//새거 넣어서 붙임
			currentSkin = makeMovieClip;
			
			addSkin( currentSkin );
			
		}
		
		/**
		 *
		 */
		private function removeSkin( skin : MovieClip ):void
		{
			skin.removeEventListener(MouseEvent.MOUSE_DOWN, onBtnDown );
			skin.removeEventListener(MouseEvent.MOUSE_UP, onBtnUp );
			skin.removeEventListener(MouseEvent.MOUSE_OUT, onBtnUp );
			if( this.parent.contains( skin ) )
			{
				this.parent.removeChild( skin );
			}
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		/**
		 *
		 */
		private function tfPositioning():void
		{
			labelTF.x = currentSkin.x +(currentSkin.width - labelTF.width)/2;
			labelTF.y = currentSkin.y +(currentSkin.height - labelTF.height)/2;
		}
		
		/**
		 *
		 */
		private function addSkin( skin : MovieClip ):void
		{
			this.parent.addChild( skin );
			skin.x = this.x;
			skin.y = this.y;
			skin.width = this.width;
			skin.height = this.height;
			
			skin.addEventListener(MouseEvent.MOUSE_DOWN, onBtnDown );
			skin.addEventListener(MouseEvent.CLICK , onBtnClick );
			skin.addEventListener(MouseEvent.MOUSE_UP, onBtnUp );
			skin.addEventListener(MouseEvent.MOUSE_OUT, onBtnUp );
			
			this.parent.addChild( labelTF);
		}	
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		/**
		 *
		 */
		private function onBtnUp(e:MouseEvent):void
		{
			var currentTarget : MovieClip = e.currentTarget as MovieClip;
			currentTarget.gotoAndStop( 1 );
		}
		
		/**
		 *
		 */
		private function onBtnDown(e:MouseEvent):void
		{
			var currentTarget : MovieClip = e.currentTarget as MovieClip;
			currentTarget.gotoAndStop( 2 );
			
		}
		
		/**
		 *
		 */
		private function onBtnClick(e:MouseEvent):void
		{
			this.dispatchEvent( new MouseEvent(MouseEvent.CLICK) );
		}
		
		
	}
}